import $ from 'jquery'
import * as joint from 'jointjs'
import Element from './element'
import * as cc from '../util/constants'
/**
 * 并集节点
 */
const JointUnion = joint.shapes.basic.Union = joint.shapes.basic.Generic.extend({
    markup: '<g class="rotatable">' +
        `<rect class="background" width="${cc.ELEMENT_WIDTH}" height="${cc.ELEMENT_HEIGHT}" fill="none"/>` +
        '<path class="fill" d="M37.5,19c-0.3,0-0.5,0.2-0.5,0.5V22h7h9.5c0.3,0,0.5-0.2,0.5-0.5V19h-9H37.5z"/> ' +
        '<path class="fill" d="M43.5,16c0.3,0,0.5-0.2,0.5-0.5V13H27.5c-0.3,0-0.5,0.2-0.5,0.5V16h9H43.5z"/> ' +
        '<path class="fill" d="M27.5,25c-0.3,0-0.5,0.2-0.5,0.5V28h16.5c0.3,0,0.5-0.2,0.5-0.5V25h-7H27.5z"/> ' +
        '<path class="shadow" d="M26,18c-0.3,0-0.6-0.1-0.8-0.2c0.3,0.7,1,1.2,1.8,1.2h7v-1H26z"/> ' +
        '<path class="shadow" d="M55.8,17.2c0.1,0.3,0.2,0.5,0.2,0.8v4c0,1.1-0.9,2-2,2h-8v4c0,1.1-0.9,2-2,2H26c-0.3,0-0.6-0.1-0.8-0.2 c0.3,0.7,1,1.2,1.8,1.2h18c1.1,0,2-0.9,2-2v-4h8c1.1,0,2-0.9,2-2v-4C57,18.2,56.5,17.5,55.8,17.2z"/> ' +
        '<path class="shadow" d="M46,16h1v-3c0-0.8-0.5-1.5-1.2-1.8c0.1,0.3,0.2,0.5,0.2,0.8V16z"/> ' +
        '<path class="stroke" d="M44,10H26c-1.1,0-2,0.9-2,2v4c0,1.1,0.9,2,2,2h18c1.1,0,2-0.9,2-2v-4C46,10.9,45.1,10,44,10z M44,15.5 c0,0.3-0.2,0.5-0.5,0.5h-17c-0.3,0-0.5-0.2-0.5-0.5v-3c0-0.3,0.2-0.5,0.5-0.5h17c0.3,0,0.5,0.2,0.5,0.5V15.5z"/> ' +
        '<path class="stroke" d="M36,15h-2c-0.6,0-1-0.4-1-1v0c0-0.6,0.4-1,1-1h2c0.6,0,1,0.4,1,1v0C37,14.6,36.6,15,36,15z"/> ' +
        '<path class="stroke" d="M54,16H36c-1.1,0-2,0.9-2,2v4c0,1.1,0.9,2,2,2h18c1.1,0,2-0.9,2-2v-4C56,16.9,55.1,16,54,16z M54,21.5 c0,0.3-0.2,0.5-0.5,0.5h-17c-0.3,0-0.5-0.2-0.5-0.5v-3c0-0.3,0.2-0.5,0.5-0.5h17c0.3,0,0.5,0.2,0.5,0.5V21.5z"/> ' +
        '<path class="stroke" d="M46,21h-2c-0.6,0-1-0.4-1-1v0c0-0.6,0.4-1,1-1h2c0.6,0,1,0.4,1,1v0C47,20.6,46.6,21,46,21z"/> ' +
        '<path class="stroke" d="M44,22H26c-1.1,0-2,0.9-2,2v4c0,1.1,0.9,2,2,2h18c1.1,0,2-0.9,2-2v-4C46,22.9,45.1,22,44,22z M44,27.5 c0,0.3-0.2,0.5-0.5,0.5h-17c-0.3,0-0.5-0.2-0.5-0.5v-3c0-0.3,0.2-0.5,0.5-0.5h17c0.3,0,0.5,0.2,0.5,0.5V27.5z"/> ' +
        '<path class="stroke" d="M36,27h-2c-0.6,0-1-0.4-1-1v0c0-0.6,0.4-1,1-1h2c0.6,0,1,0.4,1,1v0C37,26.6,36.6,27,36,27z"/> ' +
        '</g>',
    defaults: joint.util.deepSupplement({
        type: 'basic.Union',
        attrs: {
            size: {
                width: cc.ELEMENT_WIDTH,
                height: cc.ELEMENT_HEIGHT,
            },
            ".fill": {
                fill: "none"
            },
            ".shadow": {
                'opacity': 0.3
            },
            ".stroke": {},
        }
    }, joint.shapes.basic.Generic.prototype.defaults)
})

class Union extends Element {
    constructor(...args) {
        super(...args)
        this._jointInstance = new JointUnion(this.jointAttrs)
    }

    mixinAttr() {
        let mixin = {
            attrs: {
                '.stroke': {
                    fill: 'none'
                },
                '.shadow': {
                    fill: 'none'
                },
            }
        }
        $.extend(this.jointAttrs, mixin)
    }

    setColor(colorIndex) {
        super.setColor(colorIndex)
        let color = this.getColorFromIndex(colorIndex)
        this.jointInstance.attr('.stroke/fill', color)
        this.jointInstance.attr('.shadow/fill', color)
    }

    hasEnoughIncomingNodes() {
        return this.parent.precedingElements(this).length >= 2
    }

}

export default Union